<!DOCTYPE html>
<html lang=zh>
<head>
    <!-- hexo-inject:begin --><!-- hexo-inject:end --><!-- so meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="HandheldFriendly" content="True">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=5" />
    <meta name="description" content="为什么要进行多线程编程无他，更快。 多线程编程中的难点在多线程编程环境下，计算顺序的不确定性是一个本质问题。而多线程编程的难点在于  线程互斥。解决数据争用的问题。多线程编程下（同一进程的）各线程共享的全局对象，由于操作系的抢占式任务调度调度方式会造成计算顺序的混乱，具体来说就是实际运行顺序并不是程序所期待的顺序。 线程同步。将一个大任务拆分为多个小任务（线程）后，小任务之间是需要通过某种方式组织">
<meta property="og:type" content="article">
<meta property="og:title" content="C++多线程同步与互斥基本原语">
<meta property="og:url" content="https://hillbamboo.gitee.io/2019/12/09/cxx-thread-sync/index.html">
<meta property="og:site_name" content="拿铁轮的自留地">
<meta property="og:description" content="为什么要进行多线程编程无他，更快。 多线程编程中的难点在多线程编程环境下，计算顺序的不确定性是一个本质问题。而多线程编程的难点在于  线程互斥。解决数据争用的问题。多线程编程下（同一进程的）各线程共享的全局对象，由于操作系的抢占式任务调度调度方式会造成计算顺序的混乱，具体来说就是实际运行顺序并不是程序所期待的顺序。 线程同步。将一个大任务拆分为多个小任务（线程）后，小任务之间是需要通过某种方式组织">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2019-12-09T15:19:26.000Z">
<meta property="article:modified_time" content="2021-05-31T23:57:54.880Z">
<meta property="article:author" content="拿铁轮">
<meta name="twitter:card" content="summary">
    
    
      
        
          <link rel="shortcut icon" href="/images/favicon.ico">
        
      
      
        
          <link rel="icon" type="image/png" href="/images/favicon-192x192.png" sizes="192x192">
        
      
      
        
          <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon.png">
        
      
    
    <!-- title -->
    <title>C++多线程同步与互斥基本原语</title>
    <!-- styles -->
    
<link rel="stylesheet" href="/css/style.css">

    <!-- persian styles -->
    
    <!-- rss -->
    
    
      <link rel="alternate" href="/true" title="拿铁轮的自留地" type="application/atom+xml" />
    
	<!-- mathjax -->
	
<meta name="generator" content="Hexo 4.2.1"><!-- hexo-inject:begin --><!-- hexo-inject:end --></head>

<body class="max-width mx-auto px3 ltr">
    
      <!-- hexo-inject:begin --><!-- hexo-inject:end --><div id="header-post">
  <a id="menu-icon" href="#" aria-label="目录"><i class="fas fa-bars fa-lg"></i></a>
  <a id="menu-icon-tablet" href="#" aria-label="目录"><i class="fas fa-bars fa-lg"></i></a>
  <a id="top-icon-tablet" href="#" aria-label="顶部" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');" style="display:none;"><i class="fas fa-chevron-up fa-lg"></i></a>
  <span id="menu">
    <span id="nav">
      <ul>
        <!--
       --><li><a href="/">首页</a></li><!--
     --><!--
       --><li><a href="/about/">关于</a></li><!--
     --><!--
       --><li><a href="/archives/">归档</a></li><!--
     --><!--
       --><li><a href="/categories/">分类</a></li><!--
     --><!--
       --><li><a href="/tags/">标签</a></li><!--
     --><!--
       --><li><a href="/search/">搜索</a></li><!--
     -->
      </ul>
    </span>
    <br/>
    <span id="actions">
      <ul>
        
        <li><a class="icon" aria-label="上一篇" href="/2021/03/27/leetcode-daily-001/"><i class="fas fa-chevron-left" aria-hidden="true" onmouseover="$('#i-prev').toggle();" onmouseout="$('#i-prev').toggle();"></i></a></li>
        
        
        <li><a class="icon" aria-label="下一篇" href="/2019/08/20/git-cannot-display-chinese/"><i class="fas fa-chevron-right" aria-hidden="true" onmouseover="$('#i-next').toggle();" onmouseout="$('#i-next').toggle();"></i></a></li>
        
        <li><a class="icon" aria-label="返回顶部" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up" aria-hidden="true" onmouseover="$('#i-top').toggle();" onmouseout="$('#i-top').toggle();"></i></a></li>
        <li><a class="icon" aria-label="分享文章" href="#"><i class="fas fa-share-alt" aria-hidden="true" onmouseover="$('#i-share').toggle();" onmouseout="$('#i-share').toggle();" onclick="$('#share').toggle();return false;"></i></a></li>
      </ul>
      <span id="i-prev" class="info" style="display:none;">上一篇</span>
      <span id="i-next" class="info" style="display:none;">下一篇</span>
      <span id="i-top" class="info" style="display:none;">返回顶部</span>
      <span id="i-share" class="info" style="display:none;">分享文章</span>
    </span>
    <br/>
    <div id="share" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=https://hillbamboo.gitee.io/2019/12/09/cxx-thread-sync/" target="_blank" rel="noopener"><i class="fab fa-facebook " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=https://hillbamboo.gitee.io/2019/12/09/cxx-thread-sync/&text=C++多线程同步与互斥基本原语" target="_blank" rel="noopener"><i class="fab fa-twitter " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=https://hillbamboo.gitee.io/2019/12/09/cxx-thread-sync/&title=C++多线程同步与互斥基本原语" target="_blank" rel="noopener"><i class="fab fa-linkedin " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://service.weibo.com/share/share.php?url=https://hillbamboo.gitee.io/2019/12/09/cxx-thread-sync/&title=C++多线程同步与互斥基本原语" target="_blank" rel="noopener"><i class="fab fa-weibo " aria-hidden="true"></i></a></li>
  <!-- <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=https://hillbamboo.gitee.io/2019/12/09/cxx-thread-sync/&is_video=false&description=C++多线程同步与互斥基本原语" target="_blank" rel="noopener"><i class="fab fa-pinterest " aria-hidden="true"></i></a></li> -->
  <li><a class="icon" href="mailto:?subject=C++多线程同步与互斥基本原语&body=Check out this article: https://hillbamboo.gitee.io/2019/12/09/cxx-thread-sync/"><i class="fas fa-envelope " aria-hidden="true"></i></a></li>
  <!-- <li><a class="icon" href="https://getpocket.com/save?url=https://hillbamboo.gitee.io/2019/12/09/cxx-thread-sync/&title=C++多线程同步与互斥基本原语" target="_blank" rel="noopener"><i class="fab fa-get-pocket " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=https://hillbamboo.gitee.io/2019/12/09/cxx-thread-sync/&title=C++多线程同步与互斥基本原语" target="_blank" rel="noopener"><i class="fab fa-reddit " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=https://hillbamboo.gitee.io/2019/12/09/cxx-thread-sync/&title=C++多线程同步与互斥基本原语" target="_blank" rel="noopener"><i class="fab fa-stumbleupon " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=https://hillbamboo.gitee.io/2019/12/09/cxx-thread-sync/&title=C++多线程同步与互斥基本原语" target="_blank" rel="noopener"><i class="fab fa-digg " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=https://hillbamboo.gitee.io/2019/12/09/cxx-thread-sync/&name=C++多线程同步与互斥基本原语&description=" target="_blank" rel="noopener"><i class="fab fa-tumblr " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://news.ycombinator.com/submitlink?u=https://hillbamboo.gitee.io/2019/12/09/cxx-thread-sync/&t=C++多线程同步与互斥基本原语" target="_blank" rel="noopener"><i class="fab fa-hacker-news " aria-hidden="true"></i></a></li> -->
</ul>



    </div>
    <div id="toc">
      <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#为什么要进行多线程编程"><span class="toc-number">1.</span> <span class="toc-text">为什么要进行多线程编程</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#多线程编程中的难点"><span class="toc-number">2.</span> <span class="toc-text">多线程编程中的难点</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#如何解决线程互斥问题"><span class="toc-number">3.</span> <span class="toc-text">如何解决线程互斥问题</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#解决数据争用问题"><span class="toc-number">4.</span> <span class="toc-text">解决数据争用问题</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#加锁操作"><span class="toc-number">4.1.</span> <span class="toc-text">加锁操作</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#跨线程安全的资源计数"><span class="toc-number">4.2.</span> <span class="toc-text">跨线程安全的资源计数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#原子操作"><span class="toc-number">4.3.</span> <span class="toc-text">原子操作</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#解决线程等待问题"><span class="toc-number">5.</span> <span class="toc-text">解决线程等待问题</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#C-高级线程同步原语"><span class="toc-number">6.</span> <span class="toc-text">C++高级线程同步原语</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#参考资料"><span class="toc-number">7.</span> <span class="toc-text">参考资料</span></a></li></ol>
    </div>
  </span>
</div>

    
    <div class="content index py4">
        
        <article class="post" itemscope itemtype="http://schema.org/BlogPosting">
  <header>
    
    <h1 class="posttitle" itemprop="name headline">
        C++多线程同步与互斥基本原语
    </h1>



    <div class="meta">
      <span class="author" itemprop="author" itemscope itemtype="http://schema.org/Person">
        <span itemprop="name">拿铁轮</span>
      </span>
      
    <div class="postdate">
      
        <time datetime="2019-12-09T15:19:26.000Z" itemprop="datePublished">2019-12-09</time>
        
      
    </div>


      

      

    </div>
  </header>
  

  <div class="content" itemprop="articleBody">
    <h1 id="为什么要进行多线程编程"><a href="#为什么要进行多线程编程" class="headerlink" title="为什么要进行多线程编程"></a>为什么要进行多线程编程</h1><p>无他，更快。</p>
<h1 id="多线程编程中的难点"><a href="#多线程编程中的难点" class="headerlink" title="多线程编程中的难点"></a>多线程编程中的难点</h1><p>在多线程编程环境下，计算顺序的不确定性是一个本质问题。而多线程编程的难点在于</p>
<ol>
<li>线程互斥。解决数据争用的问题。多线程编程下（同一进程的）各线程共享的全局对象，由于操作系的抢占式任务调度调度方式会造成计算顺序的混乱，具体来说就是实际运行顺序并不是程序所期待的顺序。</li>
<li>线程同步。将一个大任务拆分为多个小任务（线程）后，小任务之间是需要通过某种方式组织起来还原会大任务的。具体来说，小任务之间是通过一个有向无环图组织起来的，其中某些小任务需要等待另外一些小任务的完成，否则无法继续。</li>
</ol>
<h1 id="如何解决线程互斥问题"><a href="#如何解决线程互斥问题" class="headerlink" title="如何解决线程互斥问题"></a>如何解决线程互斥问题</h1><p>在C++中，解决线程互斥（问题1）的方式有：</p>
<ul>
<li>lock_gurad/unique_lock + mutex</li>
<li>semaphore = mutex + condition_variable + int_cnt</li>
<li>atomic</li>
</ul>
<p>解决问题2的方式有：</p>
<ul>
<li>condition_variable</li>
</ul>
<h1 id="解决数据争用问题"><a href="#解决数据争用问题" class="headerlink" title="解决数据争用问题"></a>解决数据争用问题</h1><h2 id="加锁操作"><a href="#加锁操作" class="headerlink" title="加锁操作"></a>加锁操作</h2><p>使用lock_gurad/unique_lock + mutex可实现异常安全的加锁操作。</p>
<p>单纯使用mutex的.lock()和.unlock()方法不是异常安全的。如每一个线程要完成<code>run()</code>这样一个计算任务：<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">mutex m;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">run</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b, <span class="keyword">int</span> c)</span> </span>&#123;</span><br><span class="line">    m.lock();</span><br><span class="line">    <span class="keyword">int</span> d = a + b / c;</span><br><span class="line">    m.unlock();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><br>这里使用的.lock()与.unlock()划定了一个临界区。若临界区中的计算抛异常了，就无法执行.unlock()语句。从而，mutex永远被锁上了。</p>
<p>上述问题的解决知道就是使用RAII手法，利用lock_guard/unique_lock持有mutex对象。lock_guard中的构造函数调用了.lock()，析构函数实现了.unlock()，从而使用lock_guard/unique_lock + mutex可实现异常安全的锁操作。具体代码如下：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">mutex m;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">run</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b, <span class="keyword">int</span> c)</span> </span>&#123;</span><br><span class="line">    lock_guard&lt;mutex&gt; l(m)</span><br><span class="line">    <span class="keyword">int</span> d = a + b / c;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="跨线程安全的资源计数"><a href="#跨线程安全的资源计数" class="headerlink" title="跨线程安全的资源计数"></a>跨线程安全的资源计数</h2><p>信号量实现了这样的功能：</p>
<ol>
<li>获得一个资源，资源计数器减1</li>
<li>释放一个资源，资源计数器加1</li>
<li>没有资源可以获得，线程阻塞等待</li>
</ol>
<p>C++原生不支持信号量，可以通过lock_guard/unique_lock + mutex + int_cnt手动实现一个。具体代码如下：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Semaphore</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    mutex mMutex;</span><br><span class="line">    condition_variable mCondVar;</span><br><span class="line">    <span class="keyword">int64_t</span> mAvailable;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    explicit Semaphore(int64_t init) : mAvailable(init)</span><br><span class="line">    &#123;&#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">post</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        &#123;</span><br><span class="line">            unique_lock&lt;mutex&gt; l(mMutex);</span><br><span class="line">            <span class="comment">// do computation</span></span><br><span class="line">            ++mAvailable;</span><br><span class="line">        &#125;</span><br><span class="line">        mCondVar.notify_one();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">wait</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        unique_lock&lt;mutex&gt; l(mMutex);</span><br><span class="line">        <span class="keyword">while</span> (mAvailable == <span class="number">0</span>) &#123;</span><br><span class="line">            mCondVar.wait(l);</span><br><span class="line">        &#125;</span><br><span class="line">        --mAvailable;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h2 id="原子操作"><a href="#原子操作" class="headerlink" title="原子操作"></a>原子操作</h2><p>c++的标准库atomic可实现原子操作。<br>todo</p>
<h1 id="解决线程等待问题"><a href="#解决线程等待问题" class="headerlink" title="解决线程等待问题"></a>解决线程等待问题</h1><p>todo</p>
<h1 id="C-高级线程同步原语"><a href="#C-高级线程同步原语" class="headerlink" title="C++高级线程同步原语"></a>C++高级线程同步原语</h1><p>future，promise，packaged_task</p>
<p>future实现延迟计算。特别地，future<void>可实现线程通知。</p>
<p>TODO</p>
<h1 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h1><p><a href="https://yq.aliyun.com/articles/584964" target="_blank" rel="noopener">10分钟，带你掌握C++多线程同步！</a></p>

  </div>
</article>

    <div class="blog-post-comments">
        <div id="disqus_thread">
            <noscript>加载评论需要在浏览器启用 JavaScript 脚本支持。</noscript>
        </div>
    </div>



        
          <div id="footer-post-container">
  <div id="footer-post">

    <div id="nav-footer" style="display: none">
      <ul>
         
          <li><a href="/">首页</a></li>
         
          <li><a href="/about/">关于</a></li>
         
          <li><a href="/archives/">归档</a></li>
         
          <li><a href="/categories/">分类</a></li>
         
          <li><a href="/tags/">标签</a></li>
         
          <li><a href="/search/">搜索</a></li>
        
      </ul>
    </div>

    <div id="toc-footer" style="display: none">
      <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#为什么要进行多线程编程"><span class="toc-number">1.</span> <span class="toc-text">为什么要进行多线程编程</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#多线程编程中的难点"><span class="toc-number">2.</span> <span class="toc-text">多线程编程中的难点</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#如何解决线程互斥问题"><span class="toc-number">3.</span> <span class="toc-text">如何解决线程互斥问题</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#解决数据争用问题"><span class="toc-number">4.</span> <span class="toc-text">解决数据争用问题</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#加锁操作"><span class="toc-number">4.1.</span> <span class="toc-text">加锁操作</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#跨线程安全的资源计数"><span class="toc-number">4.2.</span> <span class="toc-text">跨线程安全的资源计数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#原子操作"><span class="toc-number">4.3.</span> <span class="toc-text">原子操作</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#解决线程等待问题"><span class="toc-number">5.</span> <span class="toc-text">解决线程等待问题</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#C-高级线程同步原语"><span class="toc-number">6.</span> <span class="toc-text">C++高级线程同步原语</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#参考资料"><span class="toc-number">7.</span> <span class="toc-text">参考资料</span></a></li></ol>
    </div>

    <div id="share-footer" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=https://hillbamboo.gitee.io/2019/12/09/cxx-thread-sync/" target="_blank" rel="noopener"><i class="fab fa-facebook fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=https://hillbamboo.gitee.io/2019/12/09/cxx-thread-sync/&text=C++多线程同步与互斥基本原语" target="_blank" rel="noopener"><i class="fab fa-twitter fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=https://hillbamboo.gitee.io/2019/12/09/cxx-thread-sync/&title=C++多线程同步与互斥基本原语" target="_blank" rel="noopener"><i class="fab fa-linkedin fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://service.weibo.com/share/share.php?url=https://hillbamboo.gitee.io/2019/12/09/cxx-thread-sync/&title=C++多线程同步与互斥基本原语" target="_blank" rel="noopener"><i class="fab fa-weibo fa-lg" aria-hidden="true"></i></a></li>
  <!-- <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=https://hillbamboo.gitee.io/2019/12/09/cxx-thread-sync/&is_video=false&description=C++多线程同步与互斥基本原语" target="_blank" rel="noopener"><i class="fab fa-pinterest fa-lg" aria-hidden="true"></i></a></li> -->
  <li><a class="icon" href="mailto:?subject=C++多线程同步与互斥基本原语&body=Check out this article: https://hillbamboo.gitee.io/2019/12/09/cxx-thread-sync/"><i class="fas fa-envelope fa-lg" aria-hidden="true"></i></a></li>
  <!-- <li><a class="icon" href="https://getpocket.com/save?url=https://hillbamboo.gitee.io/2019/12/09/cxx-thread-sync/&title=C++多线程同步与互斥基本原语" target="_blank" rel="noopener"><i class="fab fa-get-pocket fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=https://hillbamboo.gitee.io/2019/12/09/cxx-thread-sync/&title=C++多线程同步与互斥基本原语" target="_blank" rel="noopener"><i class="fab fa-reddit fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=https://hillbamboo.gitee.io/2019/12/09/cxx-thread-sync/&title=C++多线程同步与互斥基本原语" target="_blank" rel="noopener"><i class="fab fa-stumbleupon fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=https://hillbamboo.gitee.io/2019/12/09/cxx-thread-sync/&title=C++多线程同步与互斥基本原语" target="_blank" rel="noopener"><i class="fab fa-digg fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=https://hillbamboo.gitee.io/2019/12/09/cxx-thread-sync/&name=C++多线程同步与互斥基本原语&description=" target="_blank" rel="noopener"><i class="fab fa-tumblr fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://news.ycombinator.com/submitlink?u=https://hillbamboo.gitee.io/2019/12/09/cxx-thread-sync/&t=C++多线程同步与互斥基本原语" target="_blank" rel="noopener"><i class="fab fa-hacker-news fa-lg" aria-hidden="true"></i></a></li> -->
</ul>



    </div>

    <div id="actions-footer">
        <a id="menu" class="icon" href="#" onclick="$('#nav-footer').toggle();return false;"><i class="fas fa-bars fa-lg" aria-hidden="true"></i> 菜单</a>
        <a id="toc" class="icon" href="#" onclick="$('#toc-footer').toggle();return false;"><i class="fas fa-list fa-lg" aria-hidden="true"></i> 目录</a>
        <a id="share" class="icon" href="#" onclick="$('#share-footer').toggle();return false;"><i class="fas fa-share-alt fa-lg" aria-hidden="true"></i> 分享</a>
        <a id="top" style="display:none" class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up fa-lg" aria-hidden="true"></i> 返回顶部</a>
    </div>

  </div>
</div>

        
        <footer id="footer">
  <div class="footer-left">
    Copyright &copy;
    
    
    2017-2022
    拿铁轮
  </div>
  <div class="footer-right">
    <nav>
      <ul>
        <!--
       --><li><a href="/">首页</a></li><!--
     --><!--
       --><li><a href="/about/">关于</a></li><!--
     --><!--
       --><li><a href="/archives/">归档</a></li><!--
     --><!--
       --><li><a href="/categories/">分类</a></li><!--
     --><!--
       --><li><a href="/tags/">标签</a></li><!--
     --><!--
       --><li><a href="/search/">搜索</a></li><!--
     -->
      </ul>
    </nav>
  </div>
</footer>

    </div>
    <!-- styles -->



  <link rel="preload" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" crossorigin="anonymous" onload="this.onload=null;this.rel='stylesheet'"/>


    <!-- jquery -->
 
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" crossorigin="anonymous"></script> 




<!-- clipboard -->

  
    <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.7/clipboard.min.js" crossorigin="anonymous"></script> 
  
  <script type="text/javascript">
  $(function() {
    // copy-btn HTML
    var btn = "<span class=\"btn-copy tooltipped tooltipped-sw\" aria-label=\"复制到粘贴板！\">";
    btn += '<i class="far fa-clone"></i>';
    btn += '</span>'; 
    // mount it!
    $(".highlight table").before(btn);
    var clip = new ClipboardJS('.btn-copy', {
      text: function(trigger) {
        return Array.from(trigger.nextElementSibling.querySelectorAll('.code')).reduce((str,it)=>str+it.innerText+'\n','')
      }
    });
    clip.on('success', function(e) {
      e.trigger.setAttribute('aria-label', "复制成功！");
      e.clearSelection();
    })
  })
  </script>


<script src="/js/main.js"></script>

<!-- search -->

<!-- Google Analytics -->

<!-- Baidu Analytics -->

<!-- Cloudflare Analytics -->

<!-- Umami Analytics -->

<!-- Disqus Comments -->

    <script type="text/javascript">
        var disqus_shortname = 'cactus-1';

        (function(){
            var dsq = document.createElement('script');
            dsq.type = 'text/javascript';
            dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
        }());
    </script>

<!-- utterances Comments --><!-- hexo-inject:begin --><!-- Begin: Injected MathJax -->
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({"tex2jax":{"inlineMath":[["$","$"],["\\(","\\)"]],"skipTags":["script","noscript","style","textarea","pre","code"],"processEscapes":true},"TeX":{"equationNumbers":{"autoNumber":"AMS"}}});
</script>

<script type="text/x-mathjax-config">
  MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
    for(i=0; i < all.length; i += 1) {
      all[i].SourceElement().parentNode.className += ' has-jax';
    }
  });
</script>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js">
</script>
<!-- End: Injected MathJax -->
<!-- hexo-inject:end -->

</body>
</html>
